/* 
 * File:   Display.h
 * Author: faraj
 *
 * Created on October 8, 2009, 4:38 AM
 */

#ifndef _DISPLAY_H
#define	_DISPLAY_H
#include "Globaldata.h"

void* display(void* args)
{
    CSocket *skt = (CSocket*)args;
    
    fd_set rset;
    int maxfdp1 = skt->getSockfd() + 1;
    
    FD_SET(skt->getSockfd(),&rset);
    
    int result;
    
    string *data;
 
    while(true)
    {       
       data = skt->readln();
            
       if(data == NULL)
       {
           skt->closeSocket();
           break;
       }
                 
       if(data->find(NOTIFY_QUIT) == 0)
       {
           skt->closeSocket();
           break;
       }
       else if(data->find(NOTIFY_FILE_PUT) == 0)
       {
           cout << "server : file transfer started" << endl;
           continue;
       }
       else if(data->find(NOTIFY_FILE_PUT_DONE) == 0)
       {
           vector<string> vdata;
           Tokenize(*data,vdata);
           
           cout << "server : file recieved" << endl;
           cout << " file retrival hash : " << vdata[1] << endl;
           continue;
           
       }
       else if(data->find(NOTIFY_FILE_PUT_ERROR) == 0)
       {
           cout << "server : error recieving file" << endl;
           continue;
       }
       else if(data->find(NOTIFY_FILE_GET) == 0)
       {
           vector<string> vdata;
           
           Tokenize(*data,vdata);
           
           if(vdata.size() != 3)
           {
               cout << "server response error" << endl;
               continue;
           }

           cout << "server : file sending started"<< endl;
           cout << *data << endl;
           string filename = vdata[1];

           string ssize = vdata[2];
           
           int size = atoi(ssize.c_str());
           
           cout << "recieving " << filename << "(" << size << ")" << endl;
           skt->getFile(filename,size);
           cout << "recieved " << endl;
           continue;
           
       }
       else if(data->find(NOTIFY_FILE_GET_DONE)==0)
       {
           cout << "server : file sending finished" << endl;
           continue;
       }
       else if(data->find(NOTIFY_FILE_GET_ERROR)==0)
       {
           cout << "server : file cannot be found in server" << endl;
           continue;           
       }
       cout << *data; 
    } 
}

#endif	/* _DISPLAY_H */

